package com.jumi.microservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.exception.BaseException;
import com.jm.member.common.core.utils.DelFlag;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.request.UserFollowLogUpdateRequest;
import com.jumi.microservice.entity.JmAdminUserFollowLog;
import com.jumi.microservice.service.IJmAdminUserFollowLogService;
import com.jumi.microservice.service.PageService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户跟踪日志表 前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/jm-admin-user-follow-log")
@Api(value="用户跟踪日志",tags={"用户跟踪日志"})
public class JmAdminUserFollowLogController {

    @Autowired
    IJmAdminUserFollowLogService service;


    @PostMapping("/save")
    @ApiOperation("添加跟踪日志")
    public ResponseResult saveLog(@Validated @RequestBody UserFollowLogUpdateRequest followLog) throws BaseException {
                Long adminId = ServletUtils.getLoginId();
                String adminName = ServletUtils.getLoginName();
                String backup = followLog.getBackup();
                if(backup.length()>500) return ResponseResult.error("备注内容字符不超过500!");
                JmAdminUserFollowLog log=new JmAdminUserFollowLog();
                BeanUtils.copyProperties(followLog,log);
                log.setCreateTime(LocalDateTime.now());
                log.setUpdateTime(LocalDateTime.now());
                log.setIsDel(DelFlag.normal);
                log.setAdminId(adminId);
                log.setAdminName(adminName);
        return ResponseResult.success(service.save(log));
    }


    @PostMapping("/getInfo")
    @ApiOperation("获取跟踪日志分页")
    public TableDataInfo<JmAdminUserFollowLog> getInfo
            (UserFollowLogUpdateRequest followLog,Integer size, Integer current){
        Page page=new Page(current,size);
        QueryWrapper wrapper= WrapperUtil.getWrapper(null,followLog);
        wrapper.orderByDesc("id");
        page=service.getPage(page,wrapper);
        return PageService.getDataTableInfo(page);
    }
}
